package main

import (
	"gitee.com/zhanglibin/gmall/conf"
	"gitee.com/zhanglibin/gmall/model"
	"gitee.com/zhanglibin/gmall/routers"
	// "gitee.com/zhanglibin/gmall/crontab"
	// "gitee.com/zhanglibin/xlog/log"

	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func main() {
	if conf.CheckConfig {
		return
	}

	model.MysqlConnetInit()

	// log.Debugf("start service %v", time.Now().Format("2006-01-02 15:04:05"))

	// crontab.InitCrontab()

	s := gin.Default()

	InitRouters(s)

	addr := fmt.Sprintf("%s:%d", "0.0.0.0", conf.C.App.Port)
	ListenAndServe(addr, s)

}

func InitRouters(s *gin.Engine) {
	routers.InitApiRouters(s)
	s.Any("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "ok")
	})
}

func ListenAndServe(addr string, g http.Handler) {
	s := &http.Server{
		Addr:           addr,
		Handler:        g,
		ReadTimeout:    30 * time.Second,
		WriteTimeout:   30 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	err := s.ListenAndServe() // listen and serve
	if err != nil {
		// log.Errorf("server_start_failure err:%v",err)
		panic(err)
	}
}
